package com.erp.auto.controller;


import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.erp.auto.domain.ErpAutoInfoModuleConfig;
import com.erp.auto.domain.ErpAutoModuleProductLog;
import com.erp.auto.mapper.ErpAutoInfoModuleConfigMapper;
import com.erp.auto.mapper.ErpAutoModuleProductLogMapper;
import com.erp.auto.mapper.ErpAutoSellerStoreConfigMapper;
import com.erp.auto.service.ErpAutoInfoModuleConfigService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ruoyi.common.core.domain.R;
import lombok.AllArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.temporal.ChronoUnit;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * 信息模块自动化配置
 *
 * @author WXX
 * @date 2023-12-28 10:27:55
 */
@RestController
@AllArgsConstructor
@RequestMapping("/auto/erpAutoInfoModuleConfig")
public class ErpAutoInfoModuleConfigController {

    private final ErpAutoInfoModuleConfigService erpAutoInfoModuleConfigService;
    
    /**
     * 获取待执行的自动化模块配置
     *
     * @return list
     */
    @GetMapping("/getInfoModuleConfig")
    public List<ErpAutoInfoModuleConfig> getInfoModuleConfig(@RequestParam(value = "storeId", required = false) Long storeId) {
        ErpAutoInfoModuleConfig moduleConfig = new ErpAutoInfoModuleConfig();
        moduleConfig.setStatus(1);
        if (storeId != null)
            moduleConfig.setStoreId(storeId);
        return erpAutoInfoModuleConfigService.getInfoModuleConfig(moduleConfig);
    }
    
    @GetMapping("/delect")
    public void delect(ErpAutoInfoModuleConfig moduleConfig) {
        erpAutoInfoModuleConfigService.delect(moduleConfig);
    }

    @PostMapping("/implement")
    public void implement(@RequestBody ErpAutoInfoModuleConfig moduleConfig) {
        erpAutoInfoModuleConfigService.executeCreate(moduleConfig);
    }
    
    @PostMapping("/save")
    public R<Object> save(@RequestBody ErpAutoInfoModuleConfig config) {
        int save = erpAutoInfoModuleConfigService.saveConfig(config);
        if (save > 0) {
            return R.ok();
        }
        return R.fail();
    }

    /**
     * 获取待执行的模块配置列表
     */
    @GetMapping("/getWaitExecuteList")
    public R<Object> getWaitExecuteList() {
        ErpAutoInfoModuleConfig moduleConfig = new ErpAutoInfoModuleConfig();
        moduleConfig.setStatus(1);
        List<ErpAutoInfoModuleConfig> list = erpAutoInfoModuleConfigService.getList(moduleConfig);
        return R.ok(list);
    }

    @GetMapping("/selectById")
    public R<Object> selectById(@RequestParam("id") Long id) {
        return R.ok(erpAutoInfoModuleConfigService.selectById(id));
    }

    @GetMapping("/updateStatusById")
    public R<Object> updateStatusById(@RequestParam("id") Long id) {
        int save = erpAutoInfoModuleConfigService.updateStatusById(id);
        if (save > 0) {
            return R.ok();
        }
        return R.fail();
    }

    private boolean overModelValidity(Date startTime, LocalDateTime endTime, Integer modelValidity) {
        LocalDateTime startLocal = LocalDateTime.ofInstant(startTime.toInstant(), ZoneId.systemDefault());
        long between = ChronoUnit.DAYS.between(startLocal, endTime);
        return between >= modelValidity;
    }

}
